import axios from 'axios'
import { Message } from 'element-ui'

const request = axios.create({
  // 接口1-课程设计老师提供
  // baseURL: 'http://ihrm.itheima.net/prod-api',
  // timeout: 5000
  // 接口2-对外宣传后端提供 实现 cors 跨域
  // baseURL: 'http://ihrm-java.itheima.net/api'
  baseURL: '/prod-api'
})

request.interceptors.request.use((config) => {
  return config
}, (error) => {
  return Promise.reject(error)
})

request.interceptors.response.use((response) => {
  const { data: { success, data, message }} = response
  if (!success) {
    Message.error(message)
    return Promise.reject(new Error(message))
  }
  return data
}, (error) => {
  if (error.response) Message.error(error.response.data.message)
  return Promise.reject(error)
})

export default request
